suppressPackageStartupMessages(library('BBmisc'))
#'@ suppressPackageStartupMessages(library('rmsfuns'))

pkgs <- c('knitr', 'kableExtra', 'tint', 'devtools', 'lubridate', 'data.table', 'feather', 'quantmod', 'tidyquant', 'tidyr', 'timetk', 'plyr', 'stringr', 'magrittr', 'dplyr', 'tidyverse', 'memoise', 'htmltools', 'formattable', 'zoo', 'forecast')

suppressAll(lib(pkgs))
#'@ load_pkg(pkgs)

funs <- c('glmPrice.R')
l_ply(funs, function(x) source(paste0('./function/', x)))
.cl = FALSE

options(warn = -1)#, digits.secs = 6)
rm(pkgs, funs)

1 Introduction

Today, I am trying to recall the Lasso, Elastic-Net and also Ridge models. That was the initial models I built in order to predict the stock price of LAD since few years ago.

Stock Prediction with R glmnet and tm packages

Source : shinyApp

Source : shinyApp

You are feel free to browse over shinyApp.

2 Data

mbase <- readRDS('data/LAD.rds') %>% 
  tk_tbl

3 Modelling

# ----------- eval=FALSE --------------
#'@ pr <- glmPrice(mbase, fordate = xts::last(mbase$Date)+days(1))
timeID <- mbase$index
timeID0 <- timeID %>% xts::first() %m+% years(1)
timeID %<>% .[. > timeID0]
fit <- list()
prd <- list()

for(dt in timeID) {
  for(i in 1:10) {
    smp <- mbase %>% dplyr::filter(index <= dt)
    x <- as.matrix(smp[c('USDJPY.Open', 'USDJPY.High', 'USDJPY.Low')])
    y <- smp$USDJPY.Close
    fit[[i]] <- cv.glmnet(x, y, alpha = i/10, parallel=TRUE)
    prd[[i]] <- predict(fit[[i]], newx = y, type='response', s='lambda.min')
    }
  }
dr <- dir('data', pattern = '[0-9]{8}')
md <- llply(dr, function(x) {
    pth <- paste0('data/', x, '/fitgaum.best.rds')
    readRDS(pth) %>% 
      head(1)
  })
names(md) <- dr

#'@ md$`20160201`[[1]]$yhat[[1]] %>% tail

4 MSE

dr <- dir('data', pattern = '[0-9]{8}')
comp <- llply(dr, function(x) {
    pth <- paste0('data/', x, '/fitgaum.mse1.rds')
    readRDS(pth) %>% 
      head(1) %>% 
      dplyr::rename(Submodel = model, Model = .id)
  })
names(comp) <- dr

comp %<>% 
    ldply %>% 
    tbl_df %>% 
    dplyr::rename(index = .id, MSE.1 = mse) %>% 
    mutate(index = ymd(index), 
           Model = factor(Model), 
           Submodel = factor(Submodel))
comp
## # A tibble: 517 x 4
##    index      Model      Submodel MSE.1
##    <date>     <fct>      <fct>    <dbl>
##  1 2015-01-02 fitgaum119 mse9     0.121
##  2 2015-01-05 fitgaum135 mse9     0.121
##  3 2015-01-06 fitgaum132 mse3     0.121
##  4 2015-01-07 fitgaum81  mse8     0.122
##  5 2015-01-08 fitgaum144 mse3     0.122
##  6 2015-01-09 fitgaum27  mse3     0.121
##  7 2015-01-12 fitgaum129 mse3     0.121
##  8 2015-01-13 fitgaum17  mse3     0.121
##  9 2015-01-14 fitgaum18  mse3     0.122
## 10 2015-01-15 fitgaum18  mse3     0.121
## # ... with 507 more rows
## MSE
data.frame(Model = 'Elastic-Net', MSE = mean(comp$MSE.1)) %>% 
  kable(caption = 'MSE of daily Opened and Closed Transaction Orders') %>% 
  kable_styling(bootstrap_options = c('striped', 'hover', 'condensed', 'responsive'))
MSE of daily Opened and Closed Transaction Orders
Model MSE
Elastic-Net 0.1629332

5 Conclusion

6 Appendix

6.1 Documenting File Creation

It’s useful to record some information about how your file was created.

  • File creation date: 2018-10-20
  • File latest updated date: 2018-10-21
  • R version 3.5.1 (2018-07-02)
  • R version (short form): 3.5.1
  • rmarkdown package version: 1.10
  • File version: 1.0.1
  • Author Profile: ®γσ, Eng Lian Hu
  • GitHub: Source Code
  • Additional session information:
Additional session information:
Category session_info Category Sys.info
version R version 3.5.1 (2018-07-02) sysname Windows
system x86_64, mingw32 release 10 x64
ui RTerm version build 17134
language en nodename RSTUDIO-SCIBROK
collate Japanese_Japan.932 machine x86-64
tz Asia/Tokyo login scibr
date 2018-10-21 user scibr
Current time 2018-10-21 01:25:00 JST effective_user scibr